package com.tensquare.user.controller;


import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.user.pojo.TbUser;
import com.tensquare.user.service.impl.TbUserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author XXX
 * @since 2022-10-29
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class TbUserController {
    
    @Autowired
    private TbUserServiceImpl userService;
    
    
    @ApiOperation(value = "注册用户")
    @PostMapping("/register")
    public Result register(@RequestParam String userId){
        int i= userService.register(userId);
        if (i==1){
            return new Result(true, StatusCode.OK, "注册成功", i);
        }
        return new Result(true, StatusCode.ERROR, "注册失败", i);
    }
    

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody TbUser user){
        TbUser result = userService.login(user);
        if(result != null){
            return new Result(true, StatusCode.OK, "登录成功", result);
        }
        return new Result(true, StatusCode.LOGINERROR, "登录成功",null);
    }

    @ApiOperation(value = "根据用户id查询用户")
    @PostMapping("")
    public Result selectByUserId(@RequestParam(value = "userId") String userId){
    //public Result selectByUserId(@PathVariable(value = "userId") String userId){
        TbUser user=userService.selectByUserId(userId);
        return new Result(true, StatusCode.OK,"查询成功",user);
    }

    



    
    

}
